package onion.molingo2o.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;

import onion.molingo2o.Config;
import onion.molingo2o.R;
import onion.molingo2o.db.DBHelperManager;
import onion.molingo2o.views.wheelview.ArrayWheelAdapter;
import onion.molingo2o.views.wheelview.OnWheelScrollListener;
import onion.molingo2o.views.wheelview.WheelView;

/**
 * Created by Administrator on 2015/7/24.
 */
public class AddressSelect {
    public static final String proId="proId";
    public static final String proName="proName";
    public static final String cityId="cityId";
    public static final String cityName="cityName";
    public static final String townId="townId";
    public static final String townName="townName";
    public static final String addredetail="addredetail";
    public static final String personName="personName";
    public static final String phone="phone";
    public static void initWheelCity(Context context,View cityView, String proindex,
                               String cityindex, String townindex, final Handler handler) {

        final WheelView mWheelViewPro = (WheelView) cityView
                .findViewById(R.id.provice);
        final WheelView mWheelViewCity = (WheelView) cityView
                .findViewById(R.id.city);
        final WheelView mWheelViewTwon = (WheelView) cityView
                .findViewById(R.id.area);
        Button okBtn = (Button) cityView.findViewById(R.id.ok_btn);
        Button cancelBtn = (Button) cityView.findViewById(R.id.ca_btn);
        final DBHelperManager dbManager = new DBHelperManager(context);
        map1 = dbManager.getProStringArray();
        proIndexStr = map1.get("proindex");
        proNameStr = map1.get("proname");
        // province
        // 初始化的位置--------------------------------------------------------------
        int proPosition = 0;
        if (proindex != null) {
            proPosition = getArrayPosition(proIndexStr, proindex);
        }
        map2 = dbManager.getCityStringArray(proIndexStr[proPosition]);

        cityIndexStr = map2.get("cityindex");
        cityNameStr = map2.get("cityname");
        // city
        // 初始化的位置--------------------------------------------------------------
        int cityPosition = 0;
        if (cityindex != null) {
            cityPosition = getArrayPosition(cityIndexStr, cityindex);
        }

        map3 = dbManager.getTwonStringArray(proIndexStr[proPosition],
                cityIndexStr[cityPosition]);
        twonIndexStr = map3.get("townindex");
        twonNameStr = map3.get("townname");

        // town
        // 初始化的位置--------------------------------------------------------------
        int townPosition = 0;
        if (townindex != null) {
            townPosition = getArrayPosition(twonIndexStr, townindex);
        }

        ArrayWheelAdapter<String> proAdapter = new ArrayWheelAdapter<String>(proNameStr);
        mWheelViewPro.setAdapter(proAdapter);
        Log.i("onion","proPosition"+proPosition);
        mWheelViewPro.setCurrentItem(proPosition); // 初始化province的位置----------------------------
        mWheelViewPro.setCyclic(false);
        mWheelViewPro.addScrollingListener(new OnWheelScrollListener() {

            public void onScrollingStarted(WheelView wheel) {

            }

            public void onScrollingFinished(WheelView v) {

                int proCurrentPosition = v.getCurrentItem();
                map2 = dbManager
                        .getCityStringArray(proIndexStr[proCurrentPosition]);
                cityIndexStr = map2.get("cityindex");
                cityNameStr = map2.get("cityname");
                mWheelViewCity.setAdapter(new ArrayWheelAdapter<String>(
                        cityNameStr));
                mWheelViewCity.setCurrentItem(0);

                int cityCurrentPosition = mWheelViewCity.getCurrentItem();
                map3 = dbManager.getTwonStringArray(
                        proIndexStr[proCurrentPosition],
                        cityIndexStr[cityCurrentPosition]);
                twonIndexStr = map3.get("townindex");
                twonNameStr = map3.get("townname");
                mWheelViewTwon.setAdapter(new ArrayWheelAdapter<String>(
                        twonNameStr));
                mWheelViewTwon.setCurrentItem(0);

            }
        });

        ArrayWheelAdapter<String> cityAdapter = new ArrayWheelAdapter<String>(
                cityNameStr);
        mWheelViewCity.setAdapter(cityAdapter);

        mWheelViewCity.setCurrentItem(cityPosition); // 初始化city的位置-------------------------------------------

        mWheelViewCity.addScrollingListener(new OnWheelScrollListener() {

            public void onScrollingStarted(WheelView wheel) {

            }

            public void onScrollingFinished(WheelView v) {

                int proCurrentPosition = mWheelViewPro.getCurrentItem();
                int cityCurrentPosition = v.getCurrentItem();
                map3 = dbManager.getTwonStringArray(
                        proIndexStr[proCurrentPosition],
                        cityIndexStr[cityCurrentPosition]);
                twonIndexStr = map3.get("townindex");
                twonNameStr = map3.get("townname");
                mWheelViewTwon.setAdapter(new ArrayWheelAdapter<String>(
                        twonNameStr));
                mWheelViewTwon.setCurrentItem(0);

            }
        });

        ArrayWheelAdapter<String> twonAdapter = new ArrayWheelAdapter<String>(
                twonNameStr);
        mWheelViewTwon.setAdapter(twonAdapter);

        mWheelViewTwon.setCurrentItem(townPosition); // 初始化town的位置------------------------------------------

        okBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String proIndexStr1 = proIndexStr[mWheelViewPro
                        .getCurrentItem()];
                String proName1 = proNameStr[mWheelViewPro.getCurrentItem()];
                String cityIndexStr1 = cityIndexStr[mWheelViewCity
                        .getCurrentItem()];
                String cityName1 = cityNameStr[mWheelViewCity.getCurrentItem()];
                String twonIndexStr1 = twonIndexStr[mWheelViewTwon
                        .getCurrentItem()];
                String twonName1 = twonNameStr[mWheelViewTwon.getCurrentItem()];

//                proId = proIndexStr1;
//                cityId = cityIndexStr1;
//                twonId = twonIndexStr1;
//
//                proName = proName1;
//                cityName = cityName1;
//                twonName = twonName1;
                Message message = Message.obtain();
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(proId, proIndexStr1);
                map.put(cityId, cityIndexStr1);
                map.put(townId, twonIndexStr1);
                map.put(proName, proName1);
                map.put(cityName, cityName1);
                map.put(townName, twonName1);
                message.obj = map;
                message.what= Config.COMPLETE;
                Log.i("onion", "map" + map);
                handler.sendMessage(message);

            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(Config.CANCEL);
            }
        });

    }

    private static HashMap<String, String[]> map2 = null;
    private static HashMap<String, String[]> map1 = null;
    private  static HashMap<String, String[]> map3 = null;

    private static String[] proIndexStr = null;
    private static String[] proNameStr = null;
    private static String[] cityIndexStr = null;
    private static String[] cityNameStr = null;

    private static String[] twonIndexStr = null;
    private static String[] twonNameStr = null;

    private static int getArrayPosition(String[] data, String dataIndex) {
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(dataIndex)) {
                return i;
            }
        }

        return 0;
    }

}
